<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<html>
<head>
<script type="text/javascript">
var fileList =["https://cdog01.alibaba-inc.com/aliwork/tfscom/T1fxt7FtJlXXXXXXXX_80x80","http://b.hiphotos.baidu.com/baike/w%3D268%3Bg%3D0/sign=dad7980bb3119313c743f8b65d036bea/c995d143ad4bd113fa64e1fb5bafa40f4afb0549.jpg"]


	var Downer = (function(files) {
		var h5Down = !/Trident|MSIE/.test(navigator.userAgent);
		// try{
		// 	h5Down = document.createElement("a").hasOwnProperty("download");
		// } catch(e){
		// 	h5Down = document.createElement("a").download;
		// }

		/**
		 * 在支持 download 属性的情况下使用该方法进行单个文件下载
		 * @param  {String} fileName
		 * @param  {String|FileObject} contentOrPath
		 * @return {Null}
		 */
		 var index =1;
		function downloadFile2(fileName, contentOrPath) {
			var aLink = document.createElement("a"), 
			evt = document.createEvent("MouseEvents");

			// 初始化点击事件
			evt.initEvent("click",true, true);
			// 如果是 path 或者 dataURL 直接赋值
			// 如果是 file 或者其他内容，使用 Blob 转换
			aLink.href = contentOrPath;
			// 添加文件下载名
			aLink.download = "123123123.jpg";//index+fileName;

			aLink.dispatchEvent(evt);
					
		}
		
		
		
		function downloadFile(fileName, content){
		    var aLink = document.createElement('a');
		    var blob = new Blob([content]);
		    var evt = document.createEvent("MouseEvents");
		    evt.initEvent("click", false, false);//initEvent 不加后两个参数在FF下会报错, 感谢 Barret Lee 的反馈
		    aLink.download = 'tem_'+fileName;
		    aLink.href = content;		    
		    aLink.dispatchEvent(evt);
		}
		

		/**
		 * [IEdownloadFile description]
		 * @param  {String} fileName
		 * @param  {String|FileObject} contentOrPath
		 */
		function IEdownloadFile(fileName, contentOrPath, bool) {
			var isImg = contentOrPath.slice(0, 10) === "data:image", ifr = document
					.createElement('iframe');

			ifr.style.display = 'none';
			ifr.src = contentOrPath;

			document.body.appendChild(ifr);

			// dataURL 的情况
			isImg
					&& ifr.contentWindow.document
							.write("<img src='" +
            contentOrPath + "' />");

			// 保存页面 -> 保存文件
			// alert(ifr.contentWindow.document.body.innerHTML)
			if (bool) {
				ifr.contentWindow.document.execCommand('SaveAs', false,
						fileName);
				document.body.removeChild(ifr);
			} else {
				setTimeout(function() {
					ifr.contentWindow.document.execCommand('SaveAs', false,
							fileName);
					document.body.removeChild(ifr);
				}, 0);
			}
		}

		/**
		 * [parseURL description]
		 * @param  {String} str [description]
		 * @return {String}     [description]
		 */
		function parseURL(str) {
			return str.lastIndexOf("/") > -1 ? str
					.slice(str.lastIndexOf("/") + 1) : str;
		}

		return function(files) {
			// 选择下载函数
			var downer = h5Down ? downloadFile : IEdownloadFile;

			// 判断类型，处理下载文件名
			if (files instanceof Array) {

				for (var i = 0, l = files.length; i < l; i++){
					// bug 处理
					var url = parseURL(files[i]);

				downer(url, files[i], true);
					
				}
				
			} else if (typeof files === "string") {
				downer(parseURL(files), files);
			} else {
				// 对象

				for ( var file in files)
					downer(file, files[file]);
			}
		}

	})();

	function down1() {
		Downer("../file/test.txt");
	}

	function down2() {
		Downer(fileList);
	}

	function down3() {
		Downer({
			"tmpT1fxt7FtJlXXXXXXXX.JPG" : "https://cdog01.alibaba-inc.com/aliwork/tfscom/T1fxt7FtJlXXXXXXXX_80x80",
			"2.jpg" : "http://b.hiphotos.baidu.com/baike/w%3D268%3Bg%3D0/sign=dad7980bb3119313c743f8b65d036bea/c995d143ad4bd113fa64e1fb5bafa40f4afb0549.jpg"
		});
	}
	
	
	


	function down4() {
		Downer({
			"data64.jpg" : ""
		});
	}
</script>

</head>
<body >
	<h2>Hello World22!</h2>
	<a href="javascript:" onclick="down3();return false;">多文件下载2</a>
<!-- 下载时使用"预算表.pdf"名称 -->
	<br>
	<br>

<a href="http://ost-dev.cn-hangzhou.img.aliyun-inc.com/2ffc9e60-00b3-48cc-9e88-75e0da6a55d1?spm=0.0.0.0.vu1199&Expires=1483695846&OSSAccessKeyId=LTAI1hATmDdF9lcU&Signature=b3dAi9XXdsBrfF9oS8EXiRrlX/Q%3D" download="预算表.jpg">下载支出费用表</a>
</body>
</html>
